import GameConst from "./GameConst";

export default class SoundsManager {
    constructor() {
        this.lastPlays = {};
        this.delays = {};
        this.shifts = {};
        this.musicPlayed = false;
        this.isSoundEnabled = GameConst.needMusic;
        this.isMusicEnabled = GameConst.needMusic;
        this.musicPaused = false;
        this.lastTeleport = 0;
        this.teleportCount = 0;
        this.volumes = {};
        SoundsManager.instance = this;
        this.delays["remove1"] = this.delays["remove2"] = this.delays["remove3"] = this.delays["remove4"] = GameConst.GEM_KILL_DELAY * .5;
        this.delays["cookie_crash"] = this.delays["choco_crash"] = this.delays["color_crash"] = this.delays["bush_remove"] = this.delays["root_remove"] = this.delays["water_remove"] = .05;
        this.delays["stop_move"] = .05;
        this.delays["bonus_show"] = .05;
        this.volumes["bonus_set"] = .78;
        this.volumes["choco_crash"] = 1.6;
        this.volumes["cookie_crash"] = 1.6;
        this.volumes["color_crash"] = 1;
        this.volumes["water_remove"] = .75;
        this.lastPlay = {};
    }

    getResUrl(name){
        var url =  SoundsManager.resDic[name];
        return url?url:"sound/"+name+".ogg";
    }

    soundsEnable(name){
        var t = this.lastPlay[name];
        var now = Laya.timer.currTimer;
        var play = !t || now-t>200;
        if(play){
            this.lastPlay[name] = now;
        }
        return play;
    }

    playSound (name, t) {
        if (typeof t === "undefined") {
            t = 0;
        }
        if (!this.isSoundEnabled){
            return;
        }

        if(!this.soundsEnable(name)){
            return;
        }
        var n = this.delays[name];
        var r = this.lastPlays[name];
        var i = this.shifts[name];
        var soundVolume = this.volumes[name] ? this.volumes[name] : 1;
        if (n) {
            if (!i) {
                if (r && Math.abs(this.time - r) < n)
                    return;
                else
                    this.lastPlays[name] = this.time;
            } else {
                if (!r || r < this.time)
                    this.lastPlays[name] = this.time;
                else {
                    if (r + i < this.time + n) {
                        this.lastPlays[name] = r + i;
                        s = (this.lastPlays[name] - this.time) * 1e3;
                    } else
                        return;
                }
            }
        }
        var chanel = Laya.SoundManager.playSound(this.getResUrl(name),1);
        if(chanel){
            chanel.volume = soundVolume;
        }else{
            Laya.Log.print("声音播放失败 chanel is null");
        }
    };

    playMusic() {
        if (!this.musicPlayed) {
            this.music = Laya.SoundManager.playMusic(this.getResUrl("main_music"),0);
            Laya.SoundManager.musicVolume = 0.55;
            this.musicPlayed = this.music!=null;
        }
    };
}
SoundsManager.resDic = {

    'pointer_sound':'sound/pointer.ogg',
   
    'main_music':'sound/music/btcl_main_music.ogg'
};
SoundsManager.prototype.update = function (e) {
    this.time += e;
};
SoundsManager.prototype.playTeleport = function () {
    if (GameConst.game.gameTime - this.lastTeleport > .1)
        this.teleportCount = 0;
    if (GameConst.game.gameTime > this.lastTeleport) {
        var e = this.teleportCount % 3;
        this.lastTeleport = GameConst.game.gameTime + .05;
        this.playSound("teleport" + (e + 1));
        ++this.teleportCount;
    }
};


SoundsManager.prototype.pauseMusic = function () {
    Laya.SoundManager.stopMusic();
};
SoundsManager.prototype.resumeMusic = function () {
    this.playMusic();
};
SoundsManager.prototype.setSound = function (e) {
    if (typeof e === "undefined") {
        e = true;
    }
    this.isSoundEnabled = e;
};
SoundsManager.prototype.setMusic = function (e) {
    if (typeof e === "undefined") {
        e = true;
    }
    this.isMusicEnabled = e;
    if (this.music) {
        if (e)
            this.music.resume();
        else
            this.music.pause();
    }
};
SoundsManager.prototype.reset = function () {
    this.time = 0;
    this.lastPlays = {};

};
